<?php

namespace App\Http\Controllers\Admin\UserManager;

use App\Services\Tools\Export;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

//引用http请求类
use App\Response\Response;
use Illuminate\Support\Facades\Validator;

class DeviceController extends BaseController
{
    /**
     * @api {post} /user-manager/device/ip-ban 设备列表-ip封禁解除封禁
     * @apiVersion 0.1.0
     * @apiName 会员管理-设备列表-ip封禁解除封禁
     * @apiGroup 会员管理
     * @apiParam {number} id
     * @apiParam {number} ip_state                            0被封1正常
     * @apiParam {number} remark                            封禁理由
     * @apiSuccessExample 成功响应
     *      {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function ipBan(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'id' => 'required|integer',
                'ip_state' => 'required|numeric|integer|in:0,1',
                "remark"   => 'filled|string',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $params = $request->input();
        $id = $params['id'];

        if ($params['ip_state'] == 0 && empty($params['remark'])) {
            throw new BasicException(10001, "理由不能为空！");
        }

        $res = app('repo_user');

        $rows = $res->ipBanSet($id, $params);
        if (!$rows) {
            throw new BasicException(10001, "设置失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {post} /user-manager/device/device-ban 设备列表-设备封禁解除封禁
     * @apiVersion 0.1.0
     * @apiName 会员管理-设备列表-设备封禁解除封禁
     * @apiGroup 会员管理
     * @apiParam {number} id
     * @apiParam {number} state                            0被封1正常
     * @apiParam {number} remark                            封禁理由
     * @apiSuccessExample 成功响应
     *      {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function deviceBan(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'id'  => 'required|integer',
                'state'  => 'required|numeric|integer|in:0,1',
                "remark" => 'filled|string',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $params = $request->input();
        $id = $params['id'];

        if ($params['state'] == 0 && empty($params['remark'])) {
            throw new BasicException(10001, "理由不能为空！");
        }

        $res = app('repo_user');

        $rows = $res->deviceBanSet($id, $params);
        if (!$rows) {
            throw new BasicException(10001, "设置失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /user-manager/device 设备列表
     * @apiVersion 0.1.0
     * @apiName 会员管理-设备列表
     * @apiGroup 会员管理
     * @apiParam {number} user_code 用户编号 可选
     * @apiParam {string} nickname 昵称 可选
     * @apiParam {string} ip ip 可选
     * @apiParam {number} sex 性别 1：男 2：女 可选
     * @apiParam {string} device_id 设备编号 可选
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     * @apiSuccessExample 成功响应
     * {
    "code": 200,
    "data": {
    "list": [
    {
    "id": 17992, 记录id
    "user_id": 1317661,
    "client_type": "android", 系统
    "device_id": "adae27ca27a7910d5743016cd42cc7c3", 设备编号
    "device_name": "samsungSM-F900011", 设备名称
    "ip": "60.176.146.19", ip
    "add_time": "2021-12-01 11:07:37", 上线时间
    "state": 0, 设备状态： 0：被封；1：正常
    "ip_state": 1, ip状态： 0：被封；1：正常
    "user_id": 1317661,
    "user_code": 3754625, 用户编号
    "nickname": "一霁芸3754625", 用户昵称
    "sex": 1, 性别：1：男；2：女
    "head_picture": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/avatar/11.jpg" 头像
    }
    }
    ],
    "meta": {
    "page": 1,
    "limit": "1",
    "next": "http://qipaoadmin/user-manager/device?page=2",
    "total": 3
    }
    }
    }
     */
    public function list(Request $request)
    {
        //参数校验
        $validator = Validator::make($request->all(), [
            'sex'     => 'filled|numeric|in:1,2',
            'is_emulator' => 'filled|numeric|in:1,0',
            'perPage' => 'filled|numeric',
            'page'    => 'filled|numeric',
        ]);
        if ($validator->fails()) {
            throw new BasicException(10001, $validator->errors()->first());
        }

        $list = app('repo_user')->getDeviceQueryList($request->input());

        return Response::sendList($list);
    }


    /**
     * @api {get} /user-manager/device/excel 用户设备列表导出
     * @apiVersion 0.1.0
     * @apiName 会员管理-用户设备列表导出
     * @apiGroup 会员管理
     * @apiParam {number} user_code 用户编号 可选
     * @apiParam {string} nickname 昵称 可选
     * @apiParam {string} ip ip 可选
     * @apiParam {number} sex 性别 1：男 2：女 可选
     * @apiParam {string} device_id 设备编号 可选
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "uri": "https://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/export_data/user_driver-20210926-174031-5296.xls"
     * }
     * }
     */
    public function driverToExcel(Request $request)
    {
        //参数校验
        $validator = Validator::make($request->all(), [
            'sex'     => 'filled|numeric|in:1,2',
            'is_emulator' => 'filled|numeric|in:1,0',
            'perPage' => 'filled|numeric',
            'page'    => 'filled|numeric',
        ]);
        if ($validator->fails()) {
            throw new BasicException(10001, $validator->errors()->first());
        }

        $list = app('repo_user')->getAllUserDriver($request->input());
        foreach ($list as $k => $v) {
            $list[$k]['is_emulator'] = $v['is_emulator'] ? '模拟器' : '普通设备';
        }

        $fields = [
            ['field' => 'user_code', 'header' => '用户编号', 'width' => '18'],
            ['field' => 'nickname', 'header' => '用户昵称', 'width' => '18'],
            ['field' => 'sex', 'header' => '性别', 'width' => '18'],
            ['field' => 'client_type', 'header' => '系统', 'width' => '18'],
            ['field' => 'device_id', 'header' => '设备编号', 'width' => '18'],
            ['field' => 'device_name', 'header' => '设备名称', 'width' => '18'],

            ['field' => 'ip', 'header' => 'IP', 'width' => '18'],
            ['field' => 'add_time', 'header' => '上线时间', 'width' => '18'],
            ['field' => 'state', 'header' => '设备状态', 'width' => '18'],
            ['field' => 'ip_state', 'header' => 'IP状态', 'width' => '18'],
            ['field' => 'is_emulator', 'header' => '是否是模拟器', 'width' => '18'],

        ];
        $downloadUrl = Export::exportUsePhpOffice('user_driver', $fields, $list);

        app('model_admin_export_log')->add(24, $request->input(), $downloadUrl);

        return Response::sendData(['uri' => $downloadUrl]);
    }



}
